package com.oa.lms.apps.business.absence.impl;

import java.util.Date;
import java.util.List;

import com.oa.lms.apps.business.absence.IAbsenceOrderService;
import com.oa.lms.apps.business.absence.IReplyOrderService;
import com.oa.lms.apps.business.notice.IMessageInfoService;
import com.oa.lms.apps.business.user.IUsersService;
import com.oa.lms.apps.dao.absence.IReplyOrderDao;
import com.oa.lms.apps.entity.absence.ReplyOrderValue;

public class ReplyOrderServiceImpl implements IReplyOrderService {

	private IReplyOrderDao replyOrderDao;
	
	private ReplyOrderValue replyOrderValue;
	
	private IAbsenceOrderService absenceOrderService;
	
	private IMessageInfoService messageInfoService;
	
	private IUsersService usersService;
	@Override
	public Integer add(Integer agreeOrNot, String contents,
			Integer orderId, Integer userId) {
		replyOrderValue.setAgreeOrNot(agreeOrNot);
		replyOrderValue.setContents(contents);
		
		replyOrderValue.setAbsenceOrder(absenceOrderService.queryByOrderId(orderId));
		replyOrderValue.setUserItem(usersService.queryByUserId(userId));
		replyOrderValue.setCreateTime(new Date());
		return replyOrderDao.add(replyOrderValue);
	}

	@Override
	public void modify(Integer replyId, Integer agreeOrNot, String contents,
			Date createTime, Integer orderId, Integer userId) {
		replyOrderValue = this.queryByReplyId(replyId);
		replyOrderValue.setAgreeOrNot(agreeOrNot);
		replyOrderValue.setContents(contents);
		replyOrderValue.setCreateTime(createTime);
		replyOrderValue.setAbsenceOrder(absenceOrderService.queryByOrderId(orderId));
		replyOrderValue.setUserItem(usersService.queryByUserId(userId));
		replyOrderDao.modify(replyOrderValue);

	}

	@Override
	public ReplyOrderValue queryByReplyId(Integer replyId) {
		return replyOrderDao.queryByReplyId(replyId);
	}

	@Override
	public List<ReplyOrderValue> queryReplyOrder(Integer orderId) {
		if(orderId != null)
			replyOrderValue.setAbsenceOrder(absenceOrderService.queryByOrderId(orderId));
		
		return replyOrderDao.queryReplyOrder(replyOrderValue);
	}
	
	@Override
	public void replyOrderCancelNotice(Integer orderId, String userNameInOrder, String titleInOrder){
		List<ReplyOrderValue> replyList = this.queryReplyOrder(orderId);
		if(replyList.size() != 0 && replyList != null){
			for(ReplyOrderValue replyOrderValue : replyList){
				messageInfoService.add("请假单撤销通知", userNameInOrder+"提交的["+titleInOrder+"]已撤销申请！", new Date(), replyOrderValue.getUserItem().getUserId());
			}
		}
	}

	
	//Getter and Setter

	public IReplyOrderDao getReplyOrderDao() {
		return replyOrderDao;
	}

	public void setReplyOrderDao(IReplyOrderDao replyOrderDao) {
		this.replyOrderDao = replyOrderDao;
	}

	public ReplyOrderValue getReplyOrderValue() {
		return replyOrderValue;
	}

	public void setReplyOrderValue(ReplyOrderValue replyOrderValue) {
		this.replyOrderValue = replyOrderValue;
	}

	public IAbsenceOrderService getAbsenceOrderService() {
		return absenceOrderService;
	}

	public void setAbsenceOrderService(IAbsenceOrderService absenceOrderService) {
		this.absenceOrderService = absenceOrderService;
	}

	public IUsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}

	public IMessageInfoService getMessageInfoService() {
		return messageInfoService;
	}

	public void setMessageInfoService(IMessageInfoService messageInfoService) {
		this.messageInfoService = messageInfoService;
	}
	

}

